package phan.connection;

import java.io.*;
import javax.jms.Session;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.*;
import org.apache.tomcat.util.http.fileupload.FileUpload;
import org.apache.commons.fileupload.DiskFileUpload;
import java.util.List;
import org.apache.commons.fileupload.disk.*;
import org.apache.commons.fileupload.servlet.*;
import java.util.*;
import java.io.*;
import java.io.InputStream;

/**
 * Servlet implementation class for Servlet: LoginServlet
 *
 */
public class SaveImageTest extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    /* (non-Java-doc)
     * @see javax.servlet.http.HttpServlet#HttpServlet()
     */

    public SaveImageTest() {
        super();
    }

    /* (non-Java-doc)
     * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /* (non-Java-doc)
     * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
      int i;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {

            int userID=1;
           System.out.print("hoi the gian tinh la gi"+request.getAttribute("photo1"));
            boolean isMultipart = ServletFileUpload.isMultipartContent(request);
// Create a factory for disk-based file items
            FileItemFactory factory = new DiskFileItemFactory();

// Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload(factory);

// Parse the request
            List /* FileItem */ items = upload.parseRequest(request);

// Process the uploaded items
            Iterator iter = items.iterator();

            while (iter.hasNext()) {
                FileItem item = (FileItem) iter.next();
                System.out.print("hehe: "+item);
                if (item.isFormField()) {
                    String fieldName = item.getFieldName();
                     System.out.print("true:  "+fieldName);

                } else {

                    File fullFile = new File(item.getName());
                    String fieldName = item.getFieldName();
                System.out.print("false:  "+fieldName);
                    String nom = fullFile.getName();

                    i=i+1;
                    nom = userID+String.valueOf(i)+nom;

                    System.out.print("ten:"+nom);
                    File savedFile = new File("d:/tmp/", nom);

                    item.write(savedFile);
                }

            }
             i=0;
        } catch (Exception e) {
            System.out.print(e.getMessage());
        }
    }
}